<template>
  <div class="login-container">
    <div class="login-center-container clearfix" v-loading="loading">
      <div class="login-center-left-container"></div>
      <div class="login-center-right-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
            <div class="title-container">
              <h3 class="title">登录管理平台</h3>
            </div>
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="用户名"
                name="username"
                type="text"
                clearable
                auto-complete="on"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                :type="passwordType"
                v-model="loginForm.password"
                placeholder="密码"
                name="password"
                auto-complete="on"
                @keyup.enter.native="handleLogin" />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="`${passwordType === 'password'?'eye1':'eye'}`" />
              </span>
            </el-form-item>
            <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">立即登录</el-button>
          </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', message: '用户名不可为空'}],
        password: [{required: true, trigger: 'blur', message: '密码不可为空'}]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created () {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed () {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    ...mapActions([
      'AdminLogin'
    ]),
    ...mapMutations([
      'SET_USER_NAME',
      'SET_TOKEN',
      'SET_ADMIN_ROLE',
      'SET_USER_ID'
    ]),
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.AdminLogin({username: this.loginForm.username, password: this.loginForm.password}).then((result) => {
            this.loading = false
            this.loginForm.password = ''
            if (result.state) {
              this.$router.push({ path: this.redirect || '/' })
            } else {
              this.$message.error(result.msg)
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
@bg:#f9f9f9;
@dark_gray:#889aa4;
@light_gray:#eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: @bg;
  .login-center-container
  {
    width: 1000px;
    position: absolute;
    left: 50%;
    top: 50%;
    background: white;
    border-radius: 8px;
    transform: translate(-50%, -50%);
    height: 560px;
    overflow: hidden;
    .login-center-left-container
    {
      float: left;
      width: 440px;
      height: 560px;
      text-align: center;
      position: relative;
      left: 0;
      top: 0;
      box-shadow: 2px 0 10px 0px rgba(0, 0, 0, 0.3);
      background: linear-gradient(180deg, #3AC7F5, #124186);
    }
    .login-center-right-container
    {
      float: left;
      width: 560px;
      height: 560px;
      position: relative;
      overflow: hidden;
      .login-form {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        width: 360px;
        padding: 15px 35px 15px 35px;
      }
      .title-container {
        position: relative;
        .title {
          font-size: 36px;
          line-height: 34px;
          font-weight: normal;
          color: #3AC7F5;
          margin: 0px auto 80px auto;
          text-align: center;
        }
        .set-language {
          color: #fff;
          position: absolute;
          top: 5px;
          right: 0px;
        }
      }
      .show-pwd {
        position: absolute;
        right: 10px;
        top: 0px;
        font-size: 16px;
        color: @dark_gray;
        cursor: pointer;
        user-select: none;
      }
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: @dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
}
</style>
